Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Debug][Prim][PIR]Codegen for decomp interface implementation #58451

Merged
merged 2 commits into from
Oct 30, 2023

Conversation

cyber-pioneer
Copy link
Contributor

@cyber-pioneer cyber-pioneer commented Oct 27, 2023

PR types

New features

PR changes

Others

Description

Pcard-66975
Codegen for decomp interface implementation in paddle/fluid/pir/dialect/operator/ir/op_decomp.cc

std::vector<std::vector<pir::OpResult>> MeanOp::Decomp(pir::Operation* op) {
  MeanOp op_obj = op->dyn_cast<MeanOp>();
  (void)op_obj;

  VLOG(4) << "Decomp Prepare inputs of mean";

  Tensor x(std::make_shared<primitive::LazyTensor>(op_obj.x()));

  VLOG(4) << "Decomp prepare attributes of mean";

  IntArray axis = op->attribute("axis").dyn_cast<paddle::dialect::IntArrayAttribute>().data();
  bool keepdim = op->attribute("keepdim").dyn_cast<pir::BoolAttribute>().data();

  VLOG(4) << "Decomp prepare call mean's decomp interface";

  auto org_res = op->results();
  std::vector<std::vector<pir::OpResult>> res(org_res.size());

  Tensor op_res = paddle::primitive::details::mean_decomp<primitive::LazyTensor>(x, axis, keepdim);
  res[0].push_back(
    std::static_pointer_cast<primitive::LazyTensor>(op_res.impl())
        ->value()
        .dyn_cast<pir::OpResult>());
  return res;

}


std::vector<std::vector<pir::OpResult>> LayerNormOp::Decomp(pir::Operation* op) {
  LayerNormOp op_obj = op->dyn_cast<LayerNormOp>();
  (void)op_obj;

  VLOG(4) << "Decomp Prepare inputs of layer_norm";

  Tensor x(std::make_shared<primitive::LazyTensor>(op_obj.x()));
  paddle::optional<Tensor> scale;
  if (!IsEmptyValue(op_obj.scale())){
      scale = paddle::make_optional<Tensor>(Tensor(std::make_shared<primitive::LazyTensor>(op_obj.scale())));
  }
  paddle::optional<Tensor> bias;
  if (!IsEmptyValue(op_obj.bias())){
      bias = paddle::make_optional<Tensor>(Tensor(std::make_shared<primitive::LazyTensor>(op_obj.bias())));
  }

  VLOG(4) << "Decomp prepare attributes of layer_norm";

  float epsilon = op->attribute("epsilon").dyn_cast<pir::FloatAttribute>().data();
  int begin_norm_axis = op->attribute("begin_norm_axis").dyn_cast<pir::Int32Attribute>().data();

  VLOG(4) << "Decomp prepare call layer_norm's decomp interface";

  auto org_res = op->results();
  std::vector<std::vector<pir::OpResult>> res(org_res.size());

  std::tuple<Tensor, Tensor, Tensor> op_res = paddle::primitive::details::layer_norm_decomp<primitive::LazyTensor>(
          x, scale, bias, epsilon, begin_norm_axis);
  for (size_t i = 0; i < org_res.size(); ++i) {
      res[i].push_back(std::static_pointer_cast<primitive::LazyTensor>(std::get<i>(op_res).impl())->value().dyn_cast<pir::OpResult>());
      }

  return res;

}

@paddle-bot
Copy link

paddle-bot bot commented Oct 27, 2023

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@paddle-bot paddle-bot bot added the contributor External developers label Oct 27, 2023
@paddle-bot
Copy link

paddle-bot bot commented Oct 27, 2023

❌ The PR is not created using PR's template. You can refer to this Demo.
Please use PR's template, it helps save our maintainers' time so that more developers get helped.

@cyber-pioneer cyber-pioneer changed the title [Debug][Prim][PIR]Codegen [Debug][Prim][PIR]Codegen for decomp interface implementation Oct 29, 2023
{%- set output_names=[] -%}
{%- set output_types=[] -%}
std::vector<std::vector<pir::OpResult>> {{class_name}}::Decomp(pir::Operation* op) {
{{class_name}} op_obj = op->dyn_cast<{{class_name}}>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里建议添加一个判断 op_obj 是否为空的检查

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的

@cyber-pioneer cyber-pioneer merged commit 12f42fc into PaddlePaddle:develop Oct 30, 2023
28 checks passed
f.write(content)


def filter_compat_info(items):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个函数直接从gen.py中import过来吧

)


def extend_compat_info(apis, compats):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

return apis


def process_optional_output_info(apis):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

同上

@paddle-bot paddle-bot bot removed the contributor External developers label Nov 3, 2023
zeroRains pushed a commit to zeroRains/Paddle that referenced this pull request Nov 8, 2023
danleifeng pushed a commit to danleifeng/Paddle that referenced this pull request Nov 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants